package com.mentalsurvey.service.impl;

import com.mentalsurvey.dto.QuestionBatchCreateDTO;
import com.mentalsurvey.dto.QuestionSimpleDTO;
import com.mentalsurvey.entity.Paper;
import com.mentalsurvey.entity.Question;
import com.mentalsurvey.mapper.PaperMapper;
import com.mentalsurvey.mapper.QuestionMapper;
import com.mentalsurvey.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author vito
 * @version 1.0
 */
@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private PaperMapper paperMapper;

    /**
     * 批量创建题目
     */
    @Override
    public void batchCreate(QuestionBatchCreateDTO dto) {
        Paper paper = paperMapper.selectById(dto.getPaperId());
        if (paper == null) {
            throw new IllegalArgumentException("试卷不存在，请先创建试卷");
        }
        for (QuestionSimpleDTO qdto : dto.getQuestions()) {
            Question q = new Question();
            q.setPaperId(dto.getPaperId());
            q.setText(qdto.getText());
            q.setDimension(qdto.getDimension());
            questionMapper.insert(q);
        }
    }

    /**
     * 根据试卷ID查询题目列表
     *
     * @param paperId 试卷ID
     * @return 题目列表
     */
    @Override
    public List<Question> getQuestionsByPaperId(Integer paperId) {
        return questionMapper.selectByPaperId(paperId);
    }
}
